#!/usr/bin/env python
#coding=utf8
#只做了主密码加密，项目密码加密没做

import pickle
import os
import random
import sys
import hashlib

def get_title_passwd(the_file):
    if os.path.exists(the_file):
        with open(the_file,'rb') as passwd_file:
            a = passwd_file.read().strip()
            if len(a) == 0:
                return {}
            else:
                passwd_file.seek(0)
                return pickle.load(passwd_file)
    else:
            print("项目不存在，请先创建项目")
            sys.exit(1)


"""显示所有的密码分类"""
def display():
    if os.path.exists("total.txt"):
        with open("total.txt") as total_file:
            all_total_project = total_file.readlines()
            for i in all_total_project:
                print("现有项目："+i,end="")
    else:
        print("目前没有项目")


def regester():
    display()          
    a=input("你要把密码保存在哪个项目:")
    the_file = a + ".txt"
    the_title_passwd=get_title_passwd(the_file)
    the_title=input("please input you passwd title:")
    the_passwd=input("please enter the passwd you want to save:")
    the_title_passwd[the_title]=the_passwd
    with open(the_file,'wb') as passwd_file:
        pickle.dump(the_title_passwd,passwd_file)


"""密码分类"""
def passwd_classify():
    tmp = input("Create a new project to store your password , just support english name:")
    the_project = tmp + ".txt"
    with open("total.txt","a") as total_file:
        total_file.write(the_project + "\n")
    if not os.path.exists(the_project):
        os.mknod(the_project)


"""
用户运行程序时，会判断有没有主密码文件，如果没有，会让用户创建主密码。如果有的话，会让用户输入密码，然后将这个密码加密后跟主密码文件里的密码判断，正确的话进去程序，错误的话退出
"""
def main():
    if not os.path.exists("zhu_passwd.txt"):
        the_passwd=input("please create your passwd:")
        tmp = hashlib.md5(the_passwd.encode(encoding="utf8"))
        the_encrypt_passwd =tmp.hexdigest()
        with open("zhu_passwd.txt","w") as zhu_passwd:
            zhu_passwd.write(the_encrypt_passwd)
    else:
        try_passwd = input("please input your passwd:")
        tmp = hashlib.md5(try_passwd.encode(encoding="utf8"))
        try_encrypt_passwd = tmp.hexdigest()
        with open("zhu_passwd.txt") as zhu_passwd:
            the_encrypt_passwd = zhu_passwd.read().strip()
        if try_encrypt_passwd != the_encrypt_passwd:
            print("密码错误，程序退出")
            sys.exit(1)
        print("密码正确")


"""生成随机密码,用户可以输入长度"""
def random_passwd():
    the_number = int(input("please enter the number you want to generate the password:"))
    the_passwd = ''.join(random.sample("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*()}{?/",the_number))
    print("your randomly generated password is :",the_passwd)
    

if __name__ == "__main__":
    main()
    a="""
    1、创建密码分类
    2、自动生成安全密码
    3、添加新的密码
    4、查看密码
    """
    print(a)
    choice=int(input("please input you choice:"))
    if choice == 1:
        passwd_classify()
    elif choice == 2:
        random_passwd()
    elif choice == 3:
        regester()
    elif choice == 4:
        display()
    else:
        print("输入错误，程序退出")
        sys.exit(1)